package com.webinsky.publish.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.publish.domain.InfoFilter;
import com.webinsky.publish.persist.entity.InfoEntity;

public interface InfoEntityMapper {
	@Delete({
		"delete from `info` where id = #{id, jdbcType=INTEGER}"
	})
	int deleteById(int id);
	
	@Insert({
		"insert into `info`(name, telphone, email, company, address, remark, content, del, info_date, create_at, site_id)",
		"values (#{name, jdbcType=VARCHAR}, #{telphone, jdbcType=VARCHAR}, #{email, jdbcType=VARCHAR}, #{company, jdbcType=VARCHAR}, #{address, jdbcType=VARCHAR}, #{remark, jdbcType=VARCHAR}, #{content, jdbcType=VARCHAR}, 0, now(), now(), #{siteId, jdbcType=INTEGER})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(InfoEntity info);
	
	@SelectProvider(type = InfoEntityProvider.class, method = "selectByFilter")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="telphone", property="telphone", jdbcType=JdbcType.VARCHAR),
	    @Result(column="company", property="company", jdbcType=JdbcType.VARCHAR),
	    @Result(column="address", property="address", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
	    @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="info_date", property="infoDate", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<InfoEntity> selectByFilter(@Param("filter")InfoFilter filter);
	
	@SelectProvider(type = InfoEntityProvider.class, method = "selectCountByFilter")
	int selectCountByFilter(@Param("filter")InfoFilter filter);
	
	@Select({
		"select count(*) from `info` where site_id = #{siteId, jdbcType=INTEGER}"
	})
	int selectCountBySiteId(@Param("siteId")int siteId);
}
